import * as THREE from "three";
import gsap from "gsap";
import { ref } from "vue";

let index = ref(0);
let flag = false;

// 使用键盘控制相机移动
export const handlerRoll = (camera, controls) => {
  // let _text = scenes[index.value].text
  window.addEventListener(
    "keydown",
    (e) => {
      if (flag) return;
      flag = true;
      if (e.key === "ArrowUp") {
        index.value--;
      } else if (e.key === "ArrowDown") {
        index.value++;
      }
      if (index.value >= scenes.length) {
        index.value = 0;
      } else if (index.value < 0) {
        index.value = scenes.length - 1;
      }

      scenes[index.value].callback(camera, controls);
      setTimeout(() => {
        flag = false;
      }, 1000);
    },
    false
    );
    return scenes[index.value]
};

// 使用补间动画移动相机
let timeLine1 = gsap.timeline();
let timeline2 = gsap.timeline();

// 定义相机移动函数
const translateCamera = (position, target, camera, controls) => {
  timeLine1.to(camera.position, {
    x: position.x,
    y: position.y,
    z: position.z,
    duration: 1,
    ease: "power2.inOut",
  });

  timeline2.to(controls.target, {
    x: target.x,
    y: target.y,
    z: target.z,
    duration: 1,
    ease: "power2.inOut",
  });
};

// const restoreHeart = () => {
//   let params = {
//     time: 0,
//   };

//   gsap.to(params, {
//     time: 1,
//     duration: 1,
//     onUpdate: () => {
//       for (let i = 0; i < 100; i++) {
//         let x = endArr[i].x + (starsArr[i].x - endArr[i].x) * params.time;
//         let y = endArr[i].y + (starsArr[i].y - endArr[i].y) * params.time;
//         let z = endArr[i].z + (starsArr[i].z - endArr[i].z) * params.time;
//         let matrix = new THREE.Matrix4();
//         matrix.setPosition(x, y, z);
//         starsInstance.setMatrixAt(i, matrix);
//       }
//       starsInstance.instanceMatrix.needsUpdate = true;
//     },
//   });
// };

// 实例化漫天星星
const starsInstance = new THREE.InstancedMesh(
  new THREE.SphereGeometry(0.1, 32, 32),
  new THREE.MeshStandardMaterial({
    color: 0xffffff,
    emissive: 0xffffff,
    emissiveIntensity: 10,
  }),
  100
)

// 定义场景数据
export const scenes = [
  {
    text: "圣诞快乐",
    type: 'init',
    callback: (camera, controls) => {
      // 执行函数切换位置
      translateCamera(
        new THREE.Vector3(-3.23, 3, 4.06),
        new THREE.Vector3(-8, 2, 0),
        camera,
        controls
      );
    },
  },
  {
    text: "感谢在这么大的世界里遇见了你",
    callback: (camera, controls) => {
      // 执行函数切
      translateCamera(
        new THREE.Vector3(7, 2, 23),
        new THREE.Vector3(0, 0, 0),
        camera,
        controls
      );
    },
  },
  {
    text: "祝你天天开心无烦恼",
    callback: (camera, controls) => {
      // 执行函数切
      translateCamera(
        new THREE.Vector3(-1.23, 15.98, -14.06),
        new THREE.Vector3(0, 0, 0),
        camera,
        controls
      );
      // makeHeart();
    },
  },
  {
    text: "愿与你探寻世界的每一个角落",
    callback: (camera, controls) => {
      // 执行函数切
      translateCamera(
        new THREE.Vector3(10, 3, 0),
        new THREE.Vector3(5, 2, 0),
        camera,
        controls
      );
    },
  },
  {
    text: "愿将天上的星星送给你",
    type: 'stars',
    callback: (camera, controls) => {
      // 执行函数切
      translateCamera(
        new THREE.Vector3(7, 2, 23),
        new THREE.Vector3(-3.23, 2.98, 4.06),
        camera,
        controls
      );
      // makeHeart();
    },
  },
  {
    text: "愿大家健康快乐！",
    callback: (camera, controls) => {
      // 执行函数切
      translateCamera(
        new THREE.Vector3(-20, 1.3, 6.6),
        new THREE.Vector3(5, 2, 0),
        camera,
        controls
      );
    },
  },
];
